home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / kconfigbackend.h < prev    next >
Encoding:
C/C++ Source or Header  |  2007-10-08  |  10.2 KB  |  294 lines

  1. /*
  2.    This file is part of the KDE libraries
  3.    Copyright (c) 1999 Preston Brown <pbrown@kde.org>
  4.    Portions copyright (c) 1997 Matthias Kalle Dalheimer <kalle@kde.org>
  5.  
  6.    This library is free software; you can redistribute it and/or
  7.    modify it under the terms of the GNU Library General Public
  8.    License as published by the Free Software Foundation; either
  9.    version 2 of the License, or (at your option) any later version.
  10.  
  11.    This library is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.    Library General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU Library General Public License
  17.    along with this library; see the file COPYING.LIB.  If not, write to
  18.    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  19.    Boston, MA 02110-1301, USA.
  20. */
  21.  
  22. #ifndef _KCONFIGBACKEND_H
  23. #define _KCONFIGBACKEND_H
  24.  
  25. #include "kconfigdata.h"
  26. #include <kconfigbase.h>
  27. #include <klockfile.h>
  28. #include "kdelibs_export.h"
  29.  
  30. class QFile;
  31. class KConfigBackEndPrivate;
  32.  
  33. /**
  34.  * Abstract base class for KDE configuration file loading/saving.
  35.  *
  36.  * This class forms the base for all classes that implement some
  37.  * manner of loading/saving to configuration files.  It is an
  38.  * abstract base class, meaning that you cannot directly instantiate
  39.  * objects of this class. As of right now, the only back end available
  40.  * is one to read/write to INI-style files, but in the future, other
  41.  * formats may be available, such as XML or a database.
  42.  *
  43.  * @author Preston Brown <pbrown@kde.org>,
  44.  *         Matthias Kalle Dalheimer <kalle@kde.org>
  45.  * @short KDE Configuration file loading/saving abstract base class
  46.  */
  47. class KDECORE_EXPORT KConfigBackEnd
  48. {
  49.   friend class KConfig;
  50.   friend class KSharedConfig;
  51. public:
  52.   /**
  53.    * Constructs a configuration back end.
  54.    *
  55.    * @param _config Specifies the configuration object which values
  56.    *        will be passed to as they are read, or from where values
  57.    *        to be written to will be obtained from.
  58.    * @param _fileName The name of the file in which config
  59.    *        data is stored.  All registered configuration directories
  60.    *        will be looked in in order of decreasing relevance.
  61.    * @param _resType the resource type of the fileName specified, _if_
  62.    *        it is not an absolute path (otherwise this parameter is ignored).
  63.    * @param _useKDEGlobals If true, the user's system-wide kdeglobals file
  64.    *        will be imported into the config object.  If false, only
  65.    *        the filename specified will be dealt with.
  66.    */
  67.   KConfigBackEnd(KConfigBase *_config, const QString &_fileName,
  68.          const char * _resType, bool _useKDEGlobals);
  69.  
  70.   /**
  71.    * Destructs the configuration backend.
  72.    */
  73.   virtual ~KConfigBackEnd();
  74.  
  75.   /**
  76.    * Parses all configuration files for a configuration object.  This
  77.    * method must be reimplemented by the derived classes.
  78.    *
  79.    * @returns Whether or not parsing was successful.
  80.    */
  81.   virtual bool parseConfigFiles() = 0;
  82.  
  83.   /**
  84.    * Writes configuration data to file(s).  This method must be
  85.    * reimplemented by the derived classes.
  86.    *
  87.    * @param bMerge Specifies whether the old config file already
  88.    *        on disk should be merged in with the data in memory.  If true,
  89.    *        data is read off the disk and merged.  If false, the on-disk
  90.    *        file is removed and only in-memory data is written out.
  91.    */
  92.   virtual void sync(bool bMerge = true) = 0;
  93.  
  94.   /**
  95.    * Changes the filenames associated with this back end.  You should
  96.    * probably reparse your config info after doing this.
  97.    *
  98.    * @param _fileName the new filename to use
  99.    * @param _resType the resource type of the fileName specified, _if_
  100.    *        it is not an absolute path (otherwise this parameter is ignored).
  101.    * @param _useKDEGlobals specifies whether or not to also parse the
  102.    *        global KDE configuration files.
  103.    */
  104.   void changeFileName(const QString &_fileName, const char * _resType,
  105.               bool _useKDEGlobals);
  106.  
  107.   /**
  108.    * Returns the state of the app-config object.
  109.    *
  110.    * @see KConfig::getConfigState
  111.    */
  112.   virtual KConfigBase::ConfigState getConfigState() const
  113.     { return mConfigState; }
  114.  
  115.   /**
  116.    * Returns the filename as passed to the constructor.
  117.    * @return the filename as passed to the constructor.
  118.    */
  119.   QString fileName() const { return mfileName; }
  120.  
  121.   /**
  122.    * Returns the resource type as passed to the constructor.
  123.    * @return the resource type as passed to the constructor.
  124.    */
  125.   const char * resource() const { return resType; }
  126.  
  127.   /**
  128.    * Set the locale string that defines the current language.
  129.    * @param _localeString the identifier of the language
  130.    * @see KLocale
  131.    */
  132.   void setLocaleString(const QCString &_localeString) { localeString = _localeString; }
  133.  
  134.   /**
  135.    * Set the file mode for newly created files.
  136.    * @param mode the filemode (as in chmod)
  137.    */
  138.   void setFileWriteMode(int mode);
  139.  
  140.   /**
  141.    * Check whether the config files are writable.
  142.    * @param warnUser Warn the user if the configuration files are not writable.
  143.    * @return Indicates that all of the configuration files used are writable.
  144.    * @since 3.2
  145.    */
  146.   bool checkConfigFilesWritable(bool warnUser);
  147.  
  148.   /**
  149.    * Returns a lock file object for the configuration file
  150.    * @param bGlobal If true, returns a lock file object for kdeglobals
  151.    * @since 3.3
  152.    */
  153.   KLockFile::Ptr lockFile( bool bGlobal = false );
  154.  
  155. #ifdef KDE_NO_COMPAT
  156. private:
  157. #endif
  158.   /**
  159.    * @deprecated Use fileName() instead
  160.    */
  161.   KDE_DEPRECATED QString filename() const { return mfileName; }
  162.  
  163. protected:
  164.   KConfigBase *pConfig;
  165.  
  166.   QString mfileName;
  167.   QCString resType;
  168.   bool useKDEGlobals : 1;
  169.   bool bFileImmutable : 1;
  170.   QCString localeString;
  171.   QString mLocalFileName;
  172.   QString mGlobalFileName;
  173.   KConfigBase::ConfigState mConfigState;
  174.   int mFileMode;
  175.  
  176. protected:
  177.   virtual void virtual_hook( int id, void* data );
  178. protected:
  179.   class KConfigBackEndPrivate;
  180.   KConfigBackEndPrivate *d;
  181. };
  182.  
  183.  
  184. /**
  185.  * Class for KDE INI-style configuration file loading/saving.
  186.  *
  187.  * @author Preston Brown <pbrown@kde.org>,
  188.  *         Matthias Kalle Dalheimer <kalle@kde.org>
  189.  */
  190. class KDECORE_EXPORT KConfigINIBackEnd : public KConfigBackEnd
  191. {
  192.  
  193. public:
  194.   /**
  195.    * Constructs an ini-style configuration back end.
  196.    *
  197.    * @param _config Specifies the configuration object which values
  198.    *        will be passed to as they are read, or from where values
  199.    *        to be written to will be obtained from.
  200.    * @param _fileName The name of the file in which config
  201.    *        data is stored.  All registered configuration directories
  202.    *        will be looked in in order of decreasing relevance.
  203.    * @param _resType the resource type of the fileName specified, _if_
  204.    *        it is not an absolute path (otherwise this parameter is ignored).
  205.    * @param _useKDEGlobals If true, the user's system-wide kdeglobals file
  206.    *        will be imported into the config object.  If false, only
  207.    *        the filename specified will be dealt with.
  208.    */
  209.   KConfigINIBackEnd(KConfigBase *_config, const QString &_fileName,
  210.             const char * _resType, bool _useKDEGlobals = true)
  211.     : KConfigBackEnd(_config, _fileName, _resType, _useKDEGlobals) {}
  212.  
  213.   /**
  214.    * Destructs the configuration backend.
  215.    */
  216.   virtual ~KConfigINIBackEnd() {}
  217.  
  218.   /**
  219.    * Parses all INI-style configuration files for a config object.
  220.    *
  221.    * @returns Whether or not parsing was successful.
  222.    */
  223.   bool parseConfigFiles();
  224.  
  225.   /**
  226.    * Writes configuration data to file(s).
  227.    * @param bMerge Specifies whether the old config file already
  228.    *        on disk should be merged in with the data in memory.  If true,
  229.    *        data is read off the disk and merged.  If false, the on-disk
  230.    *        file is removed and only in-memory data is written out.
  231.    */
  232.   virtual void sync(bool bMerge = true);
  233.  
  234. protected:
  235.   /**
  236.    * Parses one configuration file.
  237.    *
  238.    * @param rFile The configuration file to parse
  239.    * @param pWriteBackMap If specified, points to a KEntryMap where
  240.    *        the data read from the file should be stored, instead of
  241.    *        inserting them directly into the configuration object.
  242.    *        Use this area as a "scratchpad" when you need to know what is
  243.    *        on disk but don't want to effect the configuration object.
  244.    * @param bGlobal Specifies whether entries should be marked as
  245.    *        belonging to the global KDE configuration file rather
  246.    *        than the application-specific KDE configuration file(s).
  247.    * @param bDefault Specifies whether entries should be marked as
  248.    *        being default values.
  249.    */
  250.   void parseSingleConfigFile(QFile& rFile, KEntryMap *pWriteBackMap = 0L,
  251.                  bool bGlobal = false, bool bDefault = false);
  252.  
  253.   /**
  254.    * Writes configuration file back.
  255.    *
  256.    * @param filename The name of the file to write.
  257.    * @param bGlobal Specifies whether to write only entries which
  258.    *        are marked as belonging to the global KDE config file.
  259.    *        If this is false, it skips those entries.
  260.    * @param bMerge Specifies whether the old config file already
  261.    *        on disk should be merged in with the data in memory.  If true,
  262.    *        data is read off the disk and merged.  If false, the on-disk
  263.    *        file is removed and only in-memory data is written out.
  264.    * @return Whether some entries are left to be written to other
  265.    *         files.
  266.    */
  267.   bool writeConfigFile(QString filename, bool bGlobal = false, bool bMerge = true);
  268.  
  269.   /** Get the entry map.
  270.    *
  271.    * @param map the entries will be stored in this object.
  272.    * @param bGlobal Specifies whether to get only entries which
  273.    *        are marked as belonging to the global KDE config file.
  274.    *        If this is false, it skips those entries.
  275.    * @param mergeFile if not null, the dirty entries for this file will
  276.    * be merged.
  277.    *
  278.    * @return Whether there will be some entries left for writing to other
  279.    * files.
  280.    */
  281.   bool getEntryMap(KEntryMap &map, bool bGlobal, QFile *mergeFile);
  282.  
  283.   /** Write the entries in @e aTempMap to the file stream.*/
  284.   void writeEntries(FILE *pStream, const KEntryMap &aTempMap);
  285.  
  286. protected:
  287.   virtual void virtual_hook( int id, void* data );
  288. private:
  289.   class KConfigINIBackEndPrivate;
  290.   KConfigINIBackEndPrivate *not_d;
  291. };
  292.  
  293. #endif
  294.